package org.crazyit.activiti_d1._3_AddGroup;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.identity.Group;

import java.util.UUID;

public class AddGroup {

    public static void main(String[] args) {

        //1. 创建默认的流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();

        //2. 得到身份服务组件实例
        IdentityService identityService = engine.getIdentityService();

        //3. 生成UUID
        String genId = UUID.randomUUID().toString();

        //4. 调用newGroup() 方法创建Group 实例
        Group group = identityService.newGroup(genId);
        //错误: 将groupID 设置为null, 将抛出异常
        //Group group = identityService.newGroup(null);
        group.setName("经理组");
        group.setType("manager");
        //可以设置该条记录的ID, 但是不建议
        //group.setId("1");

        //5. 保存到数据库
        identityService.saveGroup(group);

        //6. 查询用户组
        Group data = identityService.createGroupQuery().groupId(genId).singleResult();

        //7. 修改用户组信息
        data.setName("经理2组");

        //8. 保存用户组信息
        identityService.saveGroup(data);

    }

}
